package com.eight.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.dao.AdminMapper;
import com.eight.dao.RoleMapper;
import com.eight.dao.SysAdminMapper;
import com.eight.pojo.SysAdmin;
import com.eight.pojo.SysRole;
import com.eight.service.AdminService;

import com.eight.util.JWTTool;
import com.eight.util.WebUtil;
import com.eight.vo.ChartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 业务层的实现类模板
 * @Author: Van.Planifolia
 * @CreateTime: 2023-02-22  22:01
 * @Version: 1.0
 */
@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminMapper, SysAdmin> implements AdminService {
    /**
     * 注入 adminDao层
     */
    @Autowired private AdminMapper adminMapper;
/**/
    @Autowired private SysAdminMapper sysAdminMapper;

    @Autowired private RoleMapper roleMapper;



    /**
     * 模糊分页查询
     * @param page 当前页
     * @param size 当前条数
     * @param admin 模糊的条件
     * @return 符合条件的用户 信息
     */
    @Override
    public Page<SysAdmin> selectSysAdminList(Integer page, Integer size, SysAdmin admin) {
        Page<SysAdmin> objects = new Page<>(page,size);
        String a = admin.getAdminRealname();
        Page<SysAdmin> sysAdminPage = sysAdminMapper.selectSysAdminList(a, objects);
        List<SysRole> sysRoles = roleMapper.selectList(new QueryWrapper<>());
        // 对应链表关系
        for (SysAdmin record : sysAdminPage.getRecords()) {
            for (SysRole sysRole : sysRoles) {
                if (record.getRoleId().equals(sysRole.getRoleId())) {
                    record.setRole(sysRole);
                    continue;
                }
            }
        }
        return  sysAdminPage;
    }

    /**
     * 添加新用户
     * @param admin 新用户的信息
     * @return 返回添加反馈
     */
    @Override
    public int addSysAdmin(SysAdmin admin) {
        return sysAdminMapper.insert(admin);
    }

    /**
     * 查询用户个数
     * @return 查询用户个数
     */
    @Override
    public int selectAllAdminNum() {
        return sysAdminMapper.selectAllAdminNum();
    }

    /**
     * 查询用户的性别
     * @return 返回性别
     */
    @Override
    public List<ChartVo> selectAdminSex() {
        return sysAdminMapper.selectAdminSex();
    }

    /**
     * 当前登录这信息
     * @return 返回当前登录这信息
     */
    @Override
    public SysAdmin initAdmin() {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = JWTTool.analyzeToken(token);
        QueryWrapper<SysAdmin> war = new QueryWrapper<>();
        war.eq("admin_username", (String)map.get("username"));
        return sysAdminMapper.selectOne(war);
    }


}
